package com.zzh.tree;

import java.util.LinkedList;
import java.util.Queue;

public class No111_minDepth {
    public static void main(String[] args) {
        No111Solution solution = new No111Solution();
        int i = solution.minDepth(TreeNode.initTree());
        System.out.println(i);
    }
}

class No111Solution {
    public int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        int depth = 1;
        while (!q.isEmpty()) {
            int sz=q.size();
            for (int i = 0; i <sz; i++) {
                TreeNode cur = q.poll();
                assert cur != null;
                if (cur.left == null && cur.right == null) {
                    return depth;
                }
                if (cur.left != null) {
                    q.offer(cur.left);
                }
                if (cur.right != null) {
                    q.offer(cur.right);
                }
            }
            depth++;
        }
        return depth;
    }
}
